package com.example.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.educenter.entity.UcenterMember;
import com.example.educenter.entity.vo.RegisterVo;
import com.example.educenter.mapper.UcenterMemberMapper;
import com.example.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.handler.GuliException;
import com.example.utils.JwtUtils;
import com.example.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author xiuxian
 * @since 2022-07-01
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //用户登录
    @Override
    public String login(UcenterMember ucenterMember) {
        String mobile=ucenterMember.getMobile();
        String password= ucenterMember.getPassword();

        //判断用户名和密码是否为空
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw  new GuliException(20001,"用户名或者密码为空，登录失败");
        }

        QueryWrapper<UcenterMember> memberQueryWrapper=new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",mobile);
        UcenterMember  userMember = baseMapper.selectOne(memberQueryWrapper);

        //判断对象是否为空
        if(userMember==null){
            //没有这个手机号
            throw  new GuliException(20001,"数据库没有该对象，登录失败");
        }

        //判断密码是否正确
        /**

         因为存储到数据库密码肯定加密的
         把输出的密码进行加密，再和数据库密码进行比较
         加密方式 MD5

         * */
        if(!MD5.encrypt(password).equals(userMember.getPassword())){
            throw  new GuliException(20001,"密码错误，登录失败");
        }

        //判断是否被禁用
        if(userMember.getIsDisabled()){
            throw  new GuliException(20001,"用户已被禁用登录失败");
        }

        //返回token
        String jwtToken = JwtUtils.getJwtToken(userMember.getId(), userMember.getNickname());


        return jwtToken;
    }

    //用户注册
    @Override
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();
        String code = registerVo.getCode();

        //判断上面的信息是否有空值
        if(StringUtils.isEmpty(mobile) ||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(mobile))
        {
            throw  new GuliException(20001,"基本信息不能为空，注册失败");
        }

        //判断验证码是否一样
        String mobilecode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(mobilecode)){
            throw  new GuliException(20001,"验证码错误，注册失败");
        }

        //查询数据库中是否注册了该号码
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        //查询数据库中是否有该数据
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            throw  new GuliException(20001,"该手机号已经注册了，注册失败");
        }

        //想数据库添加内容
        UcenterMember ucenterMember=new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setNickname(nickname);
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");

        baseMapper.insert(ucenterMember);


    }

    //根据openid进行查询
    @Override
    public UcenterMember getOpenIdMember(String openid) {

        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return  member;
    }

    //查询某天的注册人数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
